package com.sip.stream.utils.zfy.encoder;

import android.annotation.SuppressLint;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;
import java.nio.ByteBuffer;

public abstract class AbstractMediaCodecEncoder {
    private static final String TAG = "AbstractMediaCodecEncoder";

    protected MediaFormat cacheOutputFormate;
    protected boolean isStop;
    protected MediaCodec mediaCodec = null;
    private long lastDesTimestamp_ae = 0;
    private int fpscounter_ae = 0;

    protected abstract void stopEncode();

    public ByteBuffer copyDirectByteBuffer(ByteBuffer byteBuffer) {
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(byteBuffer.remaining());
        allocateDirect.put(byteBuffer);
        allocateDirect.order(byteBuffer.order());
        allocateDirect.position(0);
        return allocateDirect.asReadOnlyBuffer();
    }

    @SuppressLint("LongLogTag")
    protected void printInfo(ByteBuffer byteBuffer) {
        Log.i(TAG, "encodeVideoSuccess remaining" + byteBuffer.remaining() + " position" + byteBuffer.position() + " limit" + byteBuffer.limit() + " capacity" + byteBuffer.capacity() + (byteBuffer.isDirect() ? " Direct" : " Not Direct"));
    }

    @SuppressLint("LongLogTag")
    public synchronized void stopMediaCodec() {
        if (this.mediaCodec == null) {
            return;
        }
        Log.i(TAG, "mediaCodec release");
        try {
            this.mediaCodec.stop();
            this.mediaCodec.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.mediaCodec = null;
    }

    public void fpsCount(String str, String str2, long j) {
        long j2 = this.lastDesTimestamp_ae;
        if (j2 == 0) {
            this.lastDesTimestamp_ae = j;
        } else if (j - j2 > 1000000) {
            Log.i(str, "" + str2 + this.fpscounter_ae);
            this.fpscounter_ae = 0;
            this.lastDesTimestamp_ae = j;
        } else {
            this.fpscounter_ae++;
        }
    }
}
